//
//  NewsDetailViewController.m
//  GolfFriend
//
//  Created by Vescky on 14-5-7.
//  Copyright (c) 2014年 vescky.org. All rights reserved.
//

#import "NewsDetailViewController.h"
#import "SVProgressHUD.h"
#import "AppDelegate.h"
#import "UserSession.h"
#import "AppsEngine.h"
#import "ReportSpamViewController.h"
#import "ShareManager.h"

@interface NewsDetailViewController ()<UIWebViewDelegate,UIActionSheetDelegate,UITextViewDelegate> {
    
}
@end

@implementation NewsDetailViewController
@synthesize webappLink,recordId,detailInfo,coverImage,newsType;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    if (newsType == 0) {
        self.title = @"资讯详情";
    }
    else if (newsType == 1){
        self.title = @"品鉴详情";
    }
    
    waWebView.delegate = self;
    
    //定制自己的风格的  UIBarButtonItem
    UIButton* jumpButton= [[UIButton alloc] initWithFrame:CGRectMake(0, 4, 36, 36)];//
    [jumpButton setBackgroundImage:[UIImage imageNamed:@"toolbar_more.png"] forState:UIControlStateNormal];
    [jumpButton addTarget:self action:@selector(showMenu) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* actionItem= [[UIBarButtonItem alloc] initWithCustomView:jumpButton];
    [self.navigationItem setRightBarButtonItem:actionItem];
    
    [APP_DELEGATE setToolbarHidden:YES];
    [self.navigationController.navigationBar setHidden:NO];
    [super customBackButton];
    
//    if (iPhone5) {
//        waWebView.frame = CGRectMake(0, 0, 320.f, 460.f);
//    }
    
    [waWebView loadHTMLString:Web_Loading_String baseURL:nil];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [waWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:webappLink]]];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [SVProgressHUD dismiss];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UIWebViewDelegate
- (void)webViewDidStartLoad:(UIWebView *)webView {
//    [SVProgressHUD showWithStatus:@"内容加载中..."];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [SVProgressHUD dismiss];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    [SVProgressHUD dismiss];
//    [webView loadHTMLString:@"获取详细内容失败!" baseURL:nil];
}

- (void)showMenu {
    UIActionSheet *as = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"举报" otherButtonTitles:@"分享给好友", nil];
    [as showInView:self.view];
}

- (void)goBack {
    if ([waWebView canGoBack]) {
        [waWebView goBack];
    }
    else {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)publishComment {
    if (tvComment.text.length < 5) {
        [SVProgressHUD showErrorWithStatus:@"评论内容不能少于5个字"];
        return;
    }
    NSMutableDictionary *params = [[NSMutableDictionary alloc] initWithObjectsAndKeys:recordId,@"newsId",tvComment.text,@"content",[[UserSession shareUserSession] getUserId],@"userId", nil];
    [[AppsEngine shareAppsEngine] submitRequest:@"gfPublishOfficalNewsComment" param:params onCompletion:^(id jsonResponse) {
        NSLog(@"gfPublishOfficalNewsComment success");
        if ([jsonResponse objectForKey:@"status"] && [[jsonResponse objectForKey:@"status"] isEqualToString:@"success"]) {
            [SVProgressHUD showSuccessWithStatus:@"评论成功!"];
        }
    } onError:^(NSError *error) {
        
    } method:@"GET"];
}

- (IBAction)btnAction:(id)sender {
    UIButton *btn = (UIButton*)sender;
    if (btn.tag == 101) {
        //写评论
        [tvComment becomeFirstResponder];
    }
    else if (btn.tag == 102) {
//        //赞
//        if (btnSupport.selected) {
//            [SVProgressHUD showWithStatus:@"您已赞过"];
//            return;
//        }
//        NSMutableDictionary *params = [[NSMutableDictionary alloc] initWithObjectsAndKeys:recordId,@"articleId",@"news",@"type",[[UserSession shareUserSession] getUserId],@"fromId", nil];
//        [[AppsEngine shareAppsEngine] submitRequest:@"gfPublishZan" param:params onCompletion:^(id jsonResponse) {
//            NSLog(@"zan success");
//            btnSupport.selected = YES;
//        } onError:^(NSError *error) {
//            
//        } method:@"GET"];
        [[ShareManager shareManeger] shareToSocialPlatformWithUrl:webappLink title:@"名媛荟分享" text:[detailInfo objectForKey:@"title"] image:coverImage parrentViewController:self];
    }
    else if (btn.tag == 103) {
        //举报
        ReportSpamViewController *rsp = [[ReportSpamViewController alloc] init];
        [self.navigationController pushViewController:rsp animated:YES];
    }
    else if (btn.tag == 104) {
        //分享
        [[ShareManager shareManeger] shareToSocialPlatformWithUrl:webappLink title:@"名媛荟分享" text:[detailInfo objectForKey:@"title"] image:coverImage parrentViewController:self];
    }
    else if (btn.tag == 105) {
        //取消评论
        [tvComment resignFirstResponder];
    }
    else if (btn.tag == 106) {
        //发表评论
        [self publishComment];
        [tvComment resignFirstResponder];
    }
}

#pragma mark - UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0) {
        //举报
        ReportSpamViewController *rsp = [[ReportSpamViewController alloc] init];
        rsp.recordId = recordId;
        rsp.reportType = @"news";
        [self.navigationController pushViewController:rsp animated:YES];
    }
    else if (buttonIndex == 1) {
        //分享
        [[ShareManager shareManeger] shareToSocialPlatformWithUrl:webappLink title:@"名媛荟分享" text:[detailInfo objectForKey:@"title"] image:coverImage parrentViewController:self];
    }
}

#pragma mark - Responding to keyboard events
- (void)keyboardWillShow:(NSNotification *)notification {
    NSLog(@"keyboard shown");
    
    NSDictionary *userInfo = [notification userInfo];
    NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    
    viewMask.hidden = NO;
    commentPannel.hidden = NO;
    commentPannel.frame = CGRectMake(0,keyboardRect.origin.y - 170.f - 64.f, 320.f, 255.f);
    
}

- (void)keyboardWillHide:(NSNotification *)notification {
     NSLog(@"keyboard hidden");
    viewMask.hidden = YES;
    commentPannel.hidden = YES;
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
    textView.inputAccessoryView = commentPannel;
    commentPannel.hidden = NO;
    return YES;
}

@end
